package com.apinji.common.plugin;

import com.apinji.alipay.common.AlipayErrorCodeEnum;
import com.apinji.alipay.protocol.AlipayTradeResponse;
import com.apinji.common.domain.wrapper.AccessToken;
import com.apinji.common.domain.wrapper.FinalToken;
import com.apinji.common.domain.wrapper.RefreshToken;
import com.apinji.common.domain.wrapper.Reply;
import com.apinji.common.plugin.fastjson.JSONUtils;
import com.apinji.unionpay.common.UnionpayErrorCodeEnum;
import com.apinji.wxpay.common.WxPayReturnErrorEnum;

import java.sql.Timestamp;
import java.util.Arrays;

/**
 * Created by xiaxia on 2017/7/3.
 */
public class Mutual {

    /*-------------------------------------------Reply------------------------------------------------------*/
    public static Reply success(){
        Reply reply = new Reply();
        reply.setCode(200);
        reply.setMessage("访问成功");
        reply.setSuccess(true);
        return reply;
    }

    public static Reply success(Object data){
        Reply reply = new Reply();
        reply.setCode(200);
        reply.setMessage("访问成功");
        reply.setSuccess(true);
        reply.setData(data);
        return reply;
    }

    public static Reply success(Object data, Object option){
        Reply reply = new Reply();
        reply.setCode(200);
        reply.setMessage("访问成功");
        reply.setSuccess(true);
        reply.setData(data);
        reply.setOption(option);
        return reply;
    }

    public static String blocked(){
        Reply reply = new Reply();
        reply.setCode(901);
        reply.setMessage("无效参数");
        reply.setSuccess(false);
        return JSONUtils.parseJsonStr(reply);
    }

    public static Reply invalidToken(){
        Reply reply = new Reply();
        reply.setCode(900);
        reply.setMessage("无效凭证");
        reply.setSuccess(false);
        return reply;
    }

    public static Reply invalidParam(){
        Reply reply = new Reply();
        reply.setCode(901);
        reply.setMessage("无效参数");
        reply.setSuccess(false);
        return reply;
    }

    public static Reply notExist(){
        Reply reply = new Reply();
        reply.setCode(902);
        reply.setMessage("用户不存在");
        reply.setSuccess(false);
        return reply;
    }

    public static Reply incorrectPassword(){
        Reply reply = new Reply();
        reply.setCode(903);
        reply.setMessage("密码错误");
        reply.setSuccess(false);
        return reply;
    }

    public static Reply expireToken(){
        Reply reply = new Reply();
        reply.setCode(911);
        reply.setMessage("凭证过期，需刷新");
        reply.setSuccess(false);
        return reply;
    }

    public static Reply failureToken(){
        Reply reply = new Reply();
        reply.setCode(912);
        reply.setMessage("凭证失效，需重新登录");
        reply.setSuccess(false);
        return reply;
    }

    public static Reply serverError(){
        Reply reply = new Reply();
        reply.setCode(500);
        reply.setMessage("服务器解析错误");
        reply.setSuccess(false);
        return reply;
    }

    public static Reply serverError(Object obj){
        Reply reply = new Reply();
        reply.setCode(500);
        reply.setMessage("服务器解析错误");
        reply.setSuccess(false);
        reply.setOption(obj);
        return reply;
    }

    public static Reply alipayResponseError(AlipayTradeResponse response) {
        Reply reply = new Reply();
        reply.setCode(501);
        reply.setMessage(AlipayErrorCodeEnum.getMsg(response));
        reply.setSuccess(false);
        reply.setOption(response);
        return reply;
    }

    public static Reply invalidParam(String msg){
        Reply reply = new Reply();
        reply.setCode(901);
        reply.setMessage(msg);
        reply.setSuccess(false);
        return reply;
    }

    public static Reply invalidParamAppend(String msg){
        Reply reply = new Reply();
        reply.setCode(901);
        reply.setMessage("无效参数：["+msg+"]");
        reply.setSuccess(false);
        return reply;
    }

    public static Reply notExist(String msg){
        Reply reply = new Reply();
        reply.setCode(902);
        reply.setMessage(msg);
        reply.setSuccess(false);
        return reply;
    }

    public static Reply incorrectPassword(String msg){
        Reply reply = new Reply();
        reply.setCode(903);
        reply.setMessage(msg);
        reply.setSuccess(false);
        return reply;
    }

    public static Reply expireToken(String msg){
        Reply reply = new Reply();
        reply.setCode(911);
        reply.setMessage(msg);
        reply.setSuccess(false);
        return reply;
    }

    public static Reply failureToken(String msg){
        Reply reply = new Reply();
        reply.setCode(912);
        reply.setMessage(msg);
        reply.setSuccess(false);
        return reply;
    }

    public static Reply serverError(String msg){
        Reply reply = new Reply();
        reply.setCode(500);
        reply.setMessage(msg);
        reply.setSuccess(false);
        return reply;
    }


    public static Reply haveExist(){
        Reply reply = new Reply();
        reply.setCode(420);
        reply.setMessage("已经存在");
        reply.setSuccess(false);
        return reply;
    }

    public static Reply haveExist(String msg){
        Reply reply = new Reply();
        reply.setCode(420);
        reply.setMessage(msg);
        reply.setSuccess(false);
        return reply;
    }

    public static Reply haveExist(String msg, Object option){
        Reply reply = new Reply();
        reply.setCode(420);
        reply.setMessage(msg);
        reply.setSuccess(false);
        reply.setOption(option);
        return reply;
    }

    public static Reply unionpayError(String unionErrorCode) {
        Reply reply = new Reply();
        reply.setCode(421);//421表示银联端返回的错误提示
        reply.setMessage(UnionpayErrorCodeEnum.getMsg(unionErrorCode));
        reply.setSuccess(false);
        reply.setOption(unionErrorCode);
        return reply;
    }

    public static Reply wxPayError(String errCode, Object data) {
        Reply reply = new Reply();
        reply.setCode(521);//521表示微信端返回的错误提示
        reply.setMessage(WxPayReturnErrorEnum.getMsg(errCode));
        reply.setSuccess(false);
        reply.setOption(errCode);
        reply.setData(data);
        return reply;
    }

    /*-------------------------------------------Token------------------------------------------------------*/
    public static Timestamp time(int min){
        return new Timestamp(System.currentTimeMillis()+min*60*1000L);
    }

    public static FinalToken createFinalToken(String userId,String userName){
        FinalToken finalToken = new FinalToken();
        finalToken.setExpireTime(Mutual.time(1).toString());
        finalToken.setFailureTime(Mutual.time(3).toString());

        AccessToken accessToken = new AccessToken();
        accessToken.setUserId(userId);
        accessToken.setUserName(userName);
        String secret = Encode.getSecret();
        accessToken.setSecret(secret);
        finalToken.setAccessToken(accessToken);

        RefreshToken refreshToken = new RefreshToken();
        refreshToken.setUserId(userId);
        refreshToken.setSecret(Encode.getSecret());
        finalToken.setRefreshToken(refreshToken);
        return finalToken;
    }

    /*-------------------------------------------Token------------------------------------------------------*/
    public static Boolean paramChecking(Object...params){
        return Arrays.stream(params).anyMatch(param->param==null);
    }
}
